home *** CD-ROM | disk | FTP | other *** search
- /*------------------------------------------------------------------------------
- #
- # Apple Developer Technical Support
- #
- # AppleEvent Sample Control Panel Device
- #
- # AECDEV
- #
- # AECDEV.c - C Source
- # Written by C.K. Haun
- #
- # Copyright © 1991 Apple Computer, Inc.
- # All rights reserved.
- #
- # Versions: 1.0 8/91
- #
- # Components: AECdev.p August 2, 1991
- # AECDEV.PPC.c August 2, 1991
- #
- # AECDEV demonstrates the techniques needed to send AppleEvents
- # from a CDEV/DA/INIT/Driver.
- # Requires the sample AEDaemon to work.
- #
- # The techniques used in AECdev are the same you need to use to send AppleEvents
- # from any non-application piece of code, DAs/INIT/Drivers, or anything else.
- # Since the High Level Event manager will not let anyone post High Level Events
- # unless they have an event loop, your non-event looped code must rely
- # on a 'buddy' (in this case AEDaemon) to do the sending for you.
- # There's not really much more code to write (just some PPC code), it's
- # mainly a matter of moving the code from the CDEV to the backgrounder.
- #
- # Using a buddy and PPC can give you all the functionality of AppleEvents.
- ------------------------------------------------------------------------------*/
- /*------------------------------------------------------------------------------
- # This file contains the basic CDEV controlling code
- ------------------------------------------------------------------------------*/
-
- #include "AECdev.h"
-
- /* This is the main dispatcher. It must be the first code in the cdev.
- AECDEV's dispatcher responds only to the following messages from
- the Control Panel:
-
- macDev - To indicate what machines it is available on.
- initDev - To set up some temporary storage and get the caret started.
-
- */
- pascal Handle AECDEV(short message, short item, short numItems, short CPanelID, EventRecord *theEvent, Handle cdevStorage,
- DialogPtr CPDialog)
- {
- CDEVPtr temp;
- #pragma unused (CPanelID,theEvent) /* unused formal parameters */
- if (message == macDev)
- return((Handle)1); /* we work on every machine */
- else if (cdevStorage != nil) {
- switch (message) {
- long result;
- case initDev: /* initialize cdev */
- cdevStorage = NewHandle(sizeof(CDEVRec)); /* create private storage */
- /* get a PPC block. If we can't allocate one, we'll just be dead */
- if (cdevStorage) {
- HLock(cdevStorage);
- temp = (CDEVPtr)*cdevStorage;
- temp->myPPCBlock = (MyPPCRecPtr)NewPtrClear(sizeof(MyPPCRec));
- if (temp->myPPCBlock) {
- temp->myPPCBlock->myPort = (PPCPortPtr)NewPtrClear(sizeof(PPCPortRec));
- if (temp->myPPCBlock->myPort) {
- /* set the default settings for my blocks */
- temp->myPPCBlock->buffer = nil;
- temp->myPPCBlock->bufferSize = nil;
- temp->myPPCBlock->ourPort = nil;
- temp->myPPCBlock->dataToXfer = nil;
- temp->myPPCBlock->currentSessionRef = nil;
- temp->searchForTarget = false;
- temp->noBuddy = false;
- temp->notSys7 = false;
- temp->eventPending = false;
- HUnlock(cdevStorage);
- HiliteControl(SnatchHandle(CPDialog, numItems + 1), 255);
- /* check Gestalt to see if we're on an AppleEvent/PPC */
- /* capable machine machine */
- if ((Gestalt(gestaltAppleEventsAttr, &result) != noErr) ||
- (Gestalt(gestaltPPCToolboxAttr, &result) != noErr)) {
- /* no PPC or no AppleEvents. Forget it */
- temp = (CDEVPtr)*cdevStorage;
- temp->notSys7 = true;
- } else {
- /* Start PPC for us */
- if (PPCInit() == noErr) {
- FindAEBuddy((CDEVHnd)cdevStorage);/* find and launch AEBud */
- } else {
- /* PPC didn't init correctly, bail out */
- temp = (CDEVPtr)*cdevStorage;
- temp->notSys7 = true;
- }
- }
- temp = (CDEVPtr)*cdevStorage;
- /* Alert user that we con't find our Buddy */
- if (temp->noBuddy)
- StopAlert(kNoBuddyAlert, nil);
- } else { /* PPC Port rec mem check */
- /* if this fails, kill the other pointer also */
- DisposPtr((Ptr)temp->myPPCBlock);
- temp->myPPCBlock = nil;
- DisposHandle(cdevStorage);
- /* and have Control Panel manager say out of mem */
- cdevStorage = nil;
- }
- } else { /* block mem check */
- DisposHandle(cdevStorage);
- /* and have Control Panel manager say out of mem */
- cdevStorage = nil;
- }
- } /* cdevStorage mem check */
- break;
- case nulDev:
- /* check here all the time */
- /* for the port coming up if we had to launch the sender */
- /* also dim the control, as necessary */
- /* Can't do this in our completion routine, since it requires calling the */
- /* COntrol Manager and the Memory Manager */
- HLock((Handle)cdevStorage);
- temp = (CDEVPtr)*cdevStorage;
- if (temp->eventPending || (temp->searchForTarget) || temp->myPPCBlock->currentSessionRef) {
- HiliteControl(SnatchHandle(CPDialog, numItems + kSendButton), 255);
- if (!temp->notSys7)
- FindAEBuddy((CDEVHnd)cdevStorage);
- } else {
- /* if the buddy is not up yet, or if it could not be found at all */
- /* dim the button out. */
- if (temp->noBuddy || temp->notSys7)
- HiliteControl(SnatchHandle(CPDialog, numItems + kSendButton), 255);
- else
- HiliteControl(SnatchHandle(CPDialog, numItems + kSendButton), 0);
- }
- /* also, there's .... I forgot what I was going to say */
- /* Oh yeah, I remember. I also need to check here periodically to */
- /* see if the PPC calls have completed so I can dispose of the */
- /* data handle that I allocated for the PPC XFer process. */
- if (!temp->myPPCBlock->pB.openParam.portRefNum && temp->myPPCBlock->buffer != nil && !temp->noBuddy) {
- DisposPtr(temp->myPPCBlock->buffer);
- temp->myPPCBlock->buffer = nil;
- temp->eventPending = false;
- }
- HUnlock((Handle)cdevStorage);
- break;
-
- case hitDev: /* handle hit on item */
- /* They clicked in our button. Find our Buddy ( it is possible that it */
- /* terminated while we were waiting, this will re-launch it if that happened ) */
-
- if ((item - numItems) == kSendButton) {
- /* our send button. Do it now */
- FindAEBuddy((CDEVHnd)cdevStorage);
- FindATarget((CDEVHnd)cdevStorage);
- }
- break;
- case closeDev: /* clean up and dispose */
- temp = (CDEVPtr)*cdevStorage;
- if (temp->myPPCBlock->ourPort) {
- /* we have a port open. Close the port, blowing off any pending */
- /* writes or whatever */
- PPCClosePBPtr closeRec = NewPtrClear(sizeof(PPCClosePBRec));
- closeRec->ioCompletion = nil;
- closeRec->ioResult = 0;
- closeRec->portRefNum = temp->myPPCBlock->ourPort;
- PPCClose(closeRec, false);
- DisposPtr((Ptr)closeRec);
- }
- /* and kill our memory */
- if (temp->myPPCBlock->myPort)DisposPtr((Ptr)temp->myPPCBlock->myPort);
- if (temp->myPPCBlock)DisposPtr((Ptr)temp->myPPCBlock);
-
- break;
-
- case updateDev: /* handle any update drawing */
- case activDev: /* activate any needed items */
- case deactivDev: /* deactivate any needed items */
- break;
-
- case keyEvtDev: /* respond to keydown */
- break;
-
- case macDev:
- case undoDev:
- break;
-
- case cutDev:
- case copyDev:
- case pasteDev:
- case clearDev:
- break;
- }
- return(cdevStorage);
- } /* cdevStorage != nil */
-
- /*
- ** if cdevStorage = NIL then ControlPanel
- ** will put up memory error
- */
- return(nil);
- }
-
- /* Gets the ControlHandle for the item you want in the dialog box thebox. */
- /* Handy for setting checkboxes and radio buttons */
- ControlHandle SnatchHandle(DialogPtr thebox, short theGetItem)
- {
- short itemtype;
- Rect itemrect;
- Handle thandle;
- GetDItem(thebox, theGetItem, &itemtype, &thandle, &itemrect);
- return((ControlHandle)thandle);
- }
-
-
-